package com.gh.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gh.reggie.common.CustomException;
import com.gh.reggie.common.R;
import com.gh.reggie.dto.DishDto;
import com.gh.reggie.dto.SetmealDto;
import com.gh.reggie.entity.Dish;
import com.gh.reggie.entity.Setmeal;
import com.gh.reggie.entity.SetmealDish;
import com.gh.reggie.mapper.SetmealMapper;
import com.gh.reggie.service.SetmealDishService;
import com.gh.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServivceImpl extends ServiceImpl<SetmealMapper,Setmeal> implements SetmealService {

    @Autowired
    SetmealService setmealService;
    @Autowired
    SetmealDishService setmealDishService;

    public void saveWithDish(SetmealDto setmealDto) {

        this.save(setmealDto);

        //将要保存到另一张表的List单独提取出来
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //由于提取出的信息缺乏套餐id，只有菜品id，给套餐id赋值，一对多
        Long setmealId = setmealDto.getId();
        setmealDishes.stream().map((item) ->{
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐，单个和批量删除
     * @param id
     */
    public void removeWithDish(List<Long> id) {
        //查询套餐信息确认能否删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //select count(1) from setmeal  where id in () and status = 1
        queryWrapper.in(Setmeal::getId,id);
        queryWrapper.eq(Setmeal::getStatus,1);

        //setmealService.count
        int count = this.count(queryWrapper);
        if(count > 0){
            throw new CustomException("套餐正在售卖中,不能删除");
        }

        //
        this.removeByIds(id);

        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,id);

        setmealDishService.remove(lambdaQueryWrapper);

    }


    public SetmealDto getMealDtoById(Long id) {
        //获取套餐基本信息
        Setmeal setmeal = this.getById(id);
        //部分信息给Dto
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        Long setmealId = setmeal.getId();
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealId);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }


    /**
     * 更新套餐和套餐内的菜品
     * @param setmealDto
     */
    public void updateWithDish(SetmealDto setmealDto) {
        //更新套餐基本信息
        this.updateById(setmealDto);

       //根据套餐id清空之前套餐内的数据
        setmealDishService.remove(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId,setmealDto.getId()));

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);

    }
}
